package com.yl.chainStore.services.impl.sick

import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.sick.SickCustomerAccessLog
import com.yl.chainStore.mapper.sick.SickCustomerAccessLogMapper
import com.yl.chainStore.mapper.sick.SickCustomerAccessLogPlanDetailMapper
import com.yl.chainStore.mapper.sick.SickCustomerAccessLogPlanMapper
import com.yl.chainStore.services.ICustomerInfoService
import com.yl.chainStore.services.sick.ISickCustomerAccessLogService
import com.yl.chainStore.services.warehouse.IWarehouseCheckOrderService
import com.yl.chainStore.util.addTimeByType
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

/**
 * 随访，回访
 */
@Service
class SickCustomerAccessLogServiceImpl(
	private val sickCustomerAccessLogMapper: SickCustomerAccessLogMapper,
	private val customerAccessLogPlanMapper: SickCustomerAccessLogPlanMapper,
	private val customerAccessLogPlanDetailMapper: SickCustomerAccessLogPlanDetailMapper
):ISickCustomerAccessLogService {

	companion object {
		private val log = LoggerFactory.getLogger(IWarehouseCheckOrderService::class.java)
	}

	@Autowired
	private lateinit var iCustomerInfoService: ICustomerInfoService

	override fun setDealContent(id:Long,backContent:String,remark:String?,contactType:TypeEnum?): ResponseBody {
		log.info("随访，回访 ==> 完成回访：id{},backContent:{},remark:{}",id,backContent,remark)
		val sickCustomerAccessLog = sickCustomerAccessLogMapper.selectById(id) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到记录，请刷新重试"
		)
		sickCustomerAccessLog.backContent = backContent
		sickCustomerAccessLog.dealUserId = getContext().userId
		sickCustomerAccessLog.remark = remark
		if(contactType != null){
			sickCustomerAccessLog.contactType = contactType
		}

		sickCustomerAccessLogMapper.updateById(sickCustomerAccessLog)

		return returnSuccess()
	}

	override fun completeLog(id: Long): ResponseBody {
		log.info("随访，回访 ==> 完成回访状态：id{}",id)
		val sickCustomerAccessLog = sickCustomerAccessLogMapper.selectById(id) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到记录，请刷新重试"
		)
		if(sickCustomerAccessLog.startTime == null){
			sickCustomerAccessLog.realStartTime = sickCustomerAccessLog.planTime
		}
		sickCustomerAccessLog.finishTime = Date()
		sickCustomerAccessLog.status = Status.COMPLETE

		sickCustomerAccessLogMapper.updateById(sickCustomerAccessLog)
		if(sickCustomerAccessLog.planDetailId != null){
			val planDetail = customerAccessLogPlanDetailMapper.selectById(sickCustomerAccessLog.planDetailId)
			val plan = customerAccessLogPlanMapper.selectById(planDetail.planId)
			if(plan.createType == TypeEnum.SICK_CUSTOMER_ACCESS_LOG_MODEL_CREATE_TYPE_CREATE_BY_LAST_OVER){
				//继续下一次创建
				val nextAccessLogByPlanDetail = iCustomerInfoService.getNextAccessLogByPlanDetail(planDetail)
				if(nextAccessLogByPlanDetail != null){
					var standardTime = if(plan.createTimeType == TypeEnum.SICK_CUSTOMER_ACCESS_LOG_MODEL_CRATE_TIME_INTERVAL_TYPE_LAST){
						sickCustomerAccessLog.realStartTime!!
					}else{
						plan.sickTime
					}
					iCustomerInfoService.createAccessLogByPlanDetail(plan,standardTime,nextAccessLogByPlanDetail)
				}

			}
		}
		return returnSuccess()
	}

	override fun getById(id: Long): SickCustomerAccessLog? {
		return sickCustomerAccessLogMapper.selectById(id)
	}

	override fun updatePlan(accessLog: SickCustomerAccessLog): ResponseBody {
		val updateLog = sickCustomerAccessLogMapper.selectById(accessLog.id) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到记录，请刷新重试"
		)
		updateLog.dealUserId = accessLog.dealUserId
		updateLog.doctorId = accessLog.doctorId
		updateLog.consultantId = accessLog.consultantId
		updateLog.planTime = accessLog.planTime
		updateLog.planContent = accessLog.planContent
		updateLog.remark = accessLog.remark
		sickCustomerAccessLogMapper.updateById(updateLog)
		return returnSuccess()
	}

	override fun updateBackContent(accessLog: SickCustomerAccessLog): ResponseBody {
		val updateLog = sickCustomerAccessLogMapper.selectById(accessLog.id) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到记录，请刷新重试"
		)
		updateLog.contactType = accessLog.contactType
		updateLog.dealUserId = accessLog.dealUserId
		updateLog.backContent = accessLog.backContent
		updateLog.remark = accessLog.remark

		sickCustomerAccessLogMapper.updateById(updateLog)
		return returnSuccess()
	}
}
